//
//  EditTextToolBarView.m
//  SelectPhotosOrVideoContentViewLib
//
//  Created by Dubaoquan on 2019/10/25.
//

#import "TZEditTextToolBarView.h"
#import "AppConst.h"
#import "IQKeyboardManager.h"
#import "TZChooseColorView.h"
#define kHeight 100

@interface TZEditTextToolBarView()<TZChooseColorViewDelegate>
@property (nonatomic,strong) UIView *contentView;
@property (nonatomic, assign) float keyboardHeight;
@property (nonatomic, assign) double keyboardTime;
@property (nonatomic,assign) NSInteger type;
@property (nonatomic,strong) UIButton *textColorButton;
@property (nonatomic,strong) UIButton *backgroundColorButton;
@end

@implementation TZEditTextToolBarView

+(TZEditTextToolBarView *)startMonitoring
{
    __strong static TZEditTextToolBarView *inputCommentView;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        inputCommentView = [[TZEditTextToolBarView alloc] initWithFrame:CGRectMake(0, SCREENHEIGHT, SCREENWIDTH, kHeight)];

    });
    UIWindow *window = [inputCommentView getNormalWindow];
    if (inputCommentView.superview == nil) {
        
        [[NSNotificationCenter defaultCenter] addObserver:inputCommentView selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:inputCommentView selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        [window addSubview:inputCommentView];
    }
    return inputCommentView;
}

+(void)stopMonitoring{
    
    TZEditTextToolBarView *inputCommentView =  [TZEditTextToolBarView startMonitoring];
    inputCommentView.delegate = nil;
    inputCommentView.frame = CGRectMake(0, SCREENHEIGHT, SCREENWIDTH, kHeight);
    [inputCommentView removeFromSuperview];
    
    [[NSNotificationCenter defaultCenter] removeObserver:inputCommentView];
    
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:self.contentView];
        self.type = 0;
        self.userInteractionEnabled = YES;
    }
    return self;
}
- (void)layoutSubviews{
    
//    [UIView animateWithDuration:self.keyboardTime delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
//        self.contentView.frame = CGRectMake(0, self.bounds.size.height-CGRectGetHeight(self.contentView.bounds), SCREENWIDTH,CGRectGetHeight(self.contentView.bounds));
//       
//    } completion:nil];
}

-(UIWindow *)getNormalWindow {
    UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow *temp in windows) {
            if (temp.windowLevel == UIWindowLevelNormal) {
                window = temp; break;
            }
        }
    }
    return window;
}

- (void)keyboardWillShow:(NSNotification *)notification {
    
    [IQKeyboardManager sharedManager].enable = NO;
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    
    NSDictionary *userInfo = notification.userInfo;
    CGRect endFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    self.keyboardHeight = endFrame.size.height;
    NSLog(@"keyboardHeight:%f",self.keyboardHeight);
    CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    self.keyboardTime = duration;
    TZEditTextToolBarView *inputCommentView =  [TZEditTextToolBarView startMonitoring];
    inputCommentView.frame = CGRectMake(0, SCREENHEIGHT-self.keyboardHeight-self.bounds.size.height, SCREENWIDTH, kHeight);
    [self layoutIfNeeded];
    
}

- (void)keyboardWillHide:(NSNotification *)notification {
    [IQKeyboardManager sharedManager].enable = YES;
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;

    TZEditTextToolBarView *inputCommentView =  [TZEditTextToolBarView startMonitoring];
    inputCommentView.frame = CGRectMake(0, SCREENHEIGHT, SCREENWIDTH, kHeight);
    
}
-(void)hide{
    //[self.textView resignFirstResponder];
}

- (void)changeFrame:(CGFloat)height {
    
//    CGRect frame = self.textView.frame;
//    frame.size.height = height;
//    self.textView.frame = frame;
//
//    frame = self.frame;
//    frame.size.height = height+16;
//    frame.origin.y = self.bounds.size.height - frame.size.height;
//    self.contentView.frame = frame;

}
- (void)colorFromeTZChooseColorView:(UIColor *)color{
    if (self.type==0) {
        if (self.delegate) {
            [self.delegate changeTextColor:color];
            [self.textColorButton setTitleColor:color forState:UIControlStateNormal];
        }
    }else if (self.type==1){
        if (self.delegate) {
            [self.delegate changeBackgroundColor:color];
            [self.backgroundColorButton setTitleColor:color forState:UIControlStateNormal];
        }
    }
}
-(void)chooseBackgroundColor:(UIButton*)sender{
    self.type = 1;
    self.textColorButton.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.3];
    self.backgroundColorButton.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.6];
}
-(void)chooseTextColor:(UIButton*)sender{
    self.type = 0;
    self.textColorButton.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.6];
    self.backgroundColorButton.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.3];
}
-(UIView*)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, kHeight)];
        _contentView.backgroundColor  = [UIColor clearColor];//[[UIColor blackColor]colorWithAlphaComponent:0.5];
//        UIView *linewView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 2)];
//        linewView.backgroundColor = MainBackGroudColor;
//        [_contentView addSubview:linewView];
        _contentView.userInteractionEnabled = YES;
        
        UIButton *textColorButton = [UIButton buttonWithType:UIButtonTypeCustom];//[[UIButton alloc]initWithFrame:CGRectMake(10, 10, 50, 40)];
        textColorButton.frame = CGRectMake(0, 0, 80, 40);
        [textColorButton setTitle:@"字体颜色" forState:UIControlStateNormal];
        [textColorButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        textColorButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
        textColorButton.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.6];
        [textColorButton addTarget:self action:@selector(chooseTextColor:) forControlEvents:UIControlEventTouchUpInside];
        UIBezierPath *maskPath       = [UIBezierPath bezierPathWithRoundedRect:textColorButton.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(8,8)];
        CAShapeLayer *maskLayer      = [[CAShapeLayer alloc] init];
        maskLayer.frame              = textColorButton.bounds;
        maskLayer.path               = maskPath.CGPath;
        textColorButton.layer.mask                        = maskLayer;
        [_contentView addSubview:textColorButton];
        self.textColorButton = textColorButton;
        
        textColorButton = [UIButton buttonWithType:UIButtonTypeCustom];//[[UIButton alloc]initWithFrame:CGRectMake(10, 10, 50, 40)];
        textColorButton.frame = CGRectMake(80, 0, 80, 40);
        [textColorButton setTitle:@"背景颜色" forState:UIControlStateNormal];
        [textColorButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        textColorButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
        textColorButton.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.3];
        [textColorButton addTarget:self action:@selector(chooseBackgroundColor:) forControlEvents:UIControlEventTouchUpInside];
        maskPath       = [UIBezierPath bezierPathWithRoundedRect:textColorButton.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(8,8)];
        maskLayer      = [[CAShapeLayer alloc] init];
        maskLayer.frame              = textColorButton.bounds;
        maskLayer.path               = maskPath.CGPath;
        textColorButton.layer.mask                        = maskLayer;
        [_contentView addSubview:textColorButton];
        self.backgroundColorButton = textColorButton;
        
        TZChooseColorView *chooseColorView = [[TZChooseColorView alloc]initWithFrame:CGRectMake(0, 40, SCREENWIDTH, 60)];
        chooseColorView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.6];
        chooseColorView.colorDelegate = self;
        [_contentView addSubview:chooseColorView];
   
    }
    return _contentView;
}


@end
